<template>
  <base-breadcrumb>
    <searchData :searchData="searchOptions" @search="handleSearch" />
    <a-card :bordered="false">
      <!-- <title-name title="安全人员" /> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :statisticsConfig="{use: true,statistics,temp}"
        :filterData="filterData"
        :exportConfig="{ use: true,select: '导出选中',all: '导出全部',customColumns}"
      >
        <template slot="prefix">
          <a-button icon="plus" type="primary" @click="handleUpdate('add')" v-btnPermission="'hse_014_add_01'">新增
          </a-button>
          <!--          <a-button-->
          <!--            v-btnPermission="'hse_014_add_01'"-->
          <!--            :disabled="!ref.selectedRowKeys.length"-->
          <!--            type="danger"-->
          <!--            icon="delete"-->
          <!--            @click="del(ref.selectedRowKeys)"-->
          <!--          >批量删除-->
          <!--          </a-button>-->
        </template>
        <template slot="sort" slot-scope="text, record, index">{{ index + 1 }}</template>
        <template slot="auditStatusStr" slot-scope="text, record">
          <list-status :status="record.auditStatus" conversion="en" :text="text"></list-status>
        </template>
        <template slot="workCertificateStr" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="jobInformation" slot-scope="text">
          <div v-if="text && text.length>20" :title="text">{{ text.substr(0, 20) }}...</div>
          <div v-else>{{ text }}</div>
        </template>
        <template slot="actions" slot-scope="text, record">
          <MoreButton>
            <a v-btnPermission="'hse_014_loo_04'" @click="handleUpdate('look', record)">查看</a>
            <a v-btnPermission="'hse_014_edi_02'" @click="handleUpdate('edit', record)">编辑</a>
            <a v-btn-permission="'hse_014_del_03'"
                  @click="del([record.id])"
            >删除</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import {
  getSecurityPersonnelList,
  deleteSecurityPersonnel,
  getSecurityPersonnelListCount
} from '@/api/safetySupervision/securityPersonnel'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import StatusDisplay from '@/components/StatusDisplay'
import BSelect from '@/components/BComponents/BSelect'
import { getSelectListByType } from '@/api/safetySupervision/common'
import { getCurrentUserBranchCom2 } from '@/api/commons'

export default {
  name: 'a' + Date.now(),
  components: {
    StatusDisplay
  },
  data() {
    return {
      statistics: {},
      temp: () => {
        return (
          <span>
            <span>安全人员数： <a>{this.statistics.total || 0}</a>个 </span>
            <a-divider type="vertical"></a-divider>
            <span>安全讲师： <a>{this.statistics.instructorNum || 0}</a>个 </span>
            <a-divider type="vertical"></a-divider>
            <span>安全专家：<a>{this.statistics.expertNum || 0}</a>个</span>
            <a-divider type="vertical"></a-divider>
            <span>专职设备管理人员：<a>{this.statistics.fullDeviceNum || 0}</a>个</span>
            <a-divider type="vertical"></a-divider>
            <span>注册安全工程师：<a>{this.statistics.safetyEngineer || 0}</a>个</span>
          </span>
        )
      },
      filterData(e) {
        e.map(v => {
          if (v.workCertificateStr) {
            v.workCertificateStr = v.workCertificateStr.replaceAll('</br>', '，')
          } else {
            v.workCertificateStr = '/'
          }
          if (v.certificateNo) {
            v.certificateNo = v.certificateNo.replaceAll('</br>', '，')
          } else {
            v.certificateNo = '/'
          }
          if (v.firstEngineer) {
            v.firstEngineer = v.firstEngineer.replaceAll('</br>', '，')
          } else {
            v.firstEngineer = '/'
          }
        })
        return e
      },
      customColumns: [
        { title: '序号', dataIndex: 'sort' },
        { title: '分公司', dataIndex: 'branchComName' },
        { title: '项目部', dataIndex: 'projectName' },
        { title: '姓名', dataIndex: 'userName' },
        { title: '安全岗位信息', dataIndex: 'post'},
        { title: '性别', dataIndex: 'gender' },
        { title: '编号', dataIndex: 'jobNum' },
        { title: '合同类型', dataIndex: 'contractTypeStr' },
        { title: '电话', dataIndex: 'phone' },
        { title: '年龄', dataIndex: 'age' },
        { title: '学历', dataIndex: 'education' },
        { title: '专业', dataIndex: 'specialitie' },
        { title: '职称', dataIndex: 'jobTitleName' },
        { title: '岗位资格证书', dataIndex: 'workCertificateStr' },
        { title: '注册安全工程师', dataIndex: 'certificateNo' },
        { title: '一级建造师', dataIndex: 'firstEngineer' },
        { title: '工作年限', dataIndex: 'workTimeStr' },
        { title: '是否专职机械设备管理员', dataIndex: 'fullDeviceStr' },
        { title: '是否安全专家', dataIndex: 'safetyExpertStr' },
        { title: '专家类别', dataIndex: 'safetyExpertTypeStr' },
        { title: '是否安全讲师', dataIndex: 'safetyInstructorStr' },
        { title: '状态', dataIndex: 'auditStatusStr' }
      ],
      columns: [
        { title: '序号', dataIndex: 'sort', scopedSlots: { customRender: 'sort' }, width: 70 },
        { title: '姓名', dataIndex: 'userName', width: 120 },
        { title: '公司/分公司', dataIndex: 'branchComName', width: 180 },
        { title: '所属项目', dataIndex: 'projectName', width: 200 },
        { title: '安全岗位信息', dataIndex: 'post', width: 200 },
        // {
        //   title: '岗位信息', dataIndex: 'jobInformation', width: 150,
        //   scopedSlots: { customRender: 'jobInformation' }
        // },
        { title: '合同类型', dataIndex: 'contractTypeStr', width: 120 },
        {
          title: '岗位资格证', dataIndex: 'workCertificateStr',
          scopedSlots: { customRender: 'workCertificateStr' },
          width: 300
        },
        { title: '注册安全工程师', dataIndex: 'certificateNo', width: 200 },
        { title: '是否专职设备管理员', dataIndex: 'fullDeviceStr', width: 160 },
        { title: '是否安全讲师', dataIndex: 'safetyInstructorStr', width: 120 },
        { title: '安全专家类别', dataIndex: 'safetyExpertTypeStr', width: 120 },
        { title: '工作时间', dataIndex: 'workTimeStr', width: 100 },
        { title: '联系电话', dataIndex: 'phone', width: 120 },
        { title: '状态', dataIndex: 'auditStatusStr', scopedSlots: { customRender: 'auditStatusStr' }, width: 200 },
        {
          title: ' 操作',
          dataIndex: 'actions',
          width: 150,
          fixed: 'right',
          scopedSlots: { customRender: 'actions' }
        }
      ],
      queryParam: {},
      loadData: async (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        const [err, res] = await getSecurityPersonnelList(requestParameters)
        getSecurityPersonnelListCount(requestParameters).then(d => {
          const [err, res] = d
          if (!err) {
            this.statistics = res.data
          }
        })
        return new Promise((resolve, reject) => {
          if (err) {
            resolve({ code: 200, data: [] })
          } else {
            // res.data.map(v => {
            //   v.disabled = v.auditStatus === 'SUBMITTED'
            // })
            resolve(res)
          }
        })
      }
    }
  },
  created() {
    // console.log('name',this.$options.name)
  },
  computed: {
    searchOptions() {
      let searchConfig = [
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属项目',
        //   keyValue: 'branchComId',
        //   valueKey: 'projectId',
        //   type: SelectProject
        // },
        {
          name: '姓名',
          value: 'name',
          type: 'input'
        },
        {
          name: '合同类型',
          valueKey: 'contractTypeId',
          type: BSelect,
          data: [],
          init: true,
          request: async e => {
            const [err, res] = await getSelectListByType({ type: 25 })
            if (!err) {
              return { code: 200, data: res.data.filter(v => v.enabled) }
            }
          }
        },
        {
          name: '证书类型',
          valueKey: 'certificateTypeId',
          type: BSelect,
          data: [],
          init: true,
          request: async e => {
            const [err, res] = await getSelectListByType({ type: 26 })
            if (!err) {
              return { code: 200, data: res.data.filter(v => v.enabled) }
            }
          }
        },
        {
          name: '是否专职设备管理员',
          value: 'fullDevice',
          type: 'select',
          data: [
            { value: 0, label: '否' },
            { value: 1, label: '是' }
          ]
        },
        {
          name: '是否安全讲师',
          value: 'safetyInstructor',
          type: 'select',
          data: [
            { value: 0, label: '否' },
            { value: 1, label: '是' }
          ]
        },
        {
          name: '专家类别',
          valueKey: 'safetyExpertTypeId',
          type: BSelect,
          data: [],
          init: true,
          request: async e => {
            const [err, res] = await getSelectListByType({ type: 22 })
            if (!err) {
              return { code: 200, data: res.data }
            }
          }
        }
      ]
      // if (this.$currentPost().code === 'CO') {
      //   //恶心死个人，朝令夕改
      //   searchConfig.unshift(
      //     {
      //       name: '公司/分公司',
      //       valueKey: 'branchComId',
      //       type: BSelect,
      //       data: [],
      //       request: async () => {
      //         let res = await getCurrentUserBranchCom2({ tagCodes: ['BC'] })
      //         if (res.data) {
      //           res.data.map(v => {
      //             v.id = v.orgId
      //             v.name = v.orgName
      //           })
      //           console.log(res)
      //           return { code: 200, data: [{ id: this.$currentPost().orgId, name: '总部机关安监部' }, ...res.data] }
      //         }
      //         return { code: 200, data: [] }
      //       }
      //     }
      //   )
      //   // searchConfig.unshift({
      //   //   name: '公司/分公司',
      //   //   valueKey: 'branchComId',
      //   //   type: SelectSubCompany
      //   // })
      // } else {
      //   searchConfig.unshift({
      //     name: '公司/分公司',
      //     valueKey: 'branchComId',
      //     type: SelectSubCompany
      //   })
      // }
      return searchConfig
    }
  },
  methods: {
    handleSearch(val = {}) {
      this.queryParam = val
      this.$refs.table.refresh(true)
    },
    async del(keys) {
      const params = { ids: keys.join(',') }
      let fun = async q => {
        let [err, res] = await deleteSecurityPersonnel(q)
        return res.data
      }
      this.$delList2(fun, this.$refs.table.refresh,
        params)
    },
    // 处理更新操作（新增、编辑）
    async handleUpdate(type, data = {}) {
      this.$router.push({
        name: 'updateSecurityPersonnel',
        query: {
          id: data.id || '',
          pageType: type
        }
      })
    }
  }
}
</script>
